package com.order.service.imp;

import com.order.Vo.MyOrderVo;
import com.order.dao.POrderMapper;
import com.order.dao.ProductMapper;
import com.order.entity.POrder;
import com.order.entity.Product;
import com.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class OrderServiceImp implements OrderService {
    @Autowired
    POrderMapper pOrderMapper;
    @Autowired
    ProductMapper productMapper;


    //通过当前用户Id和状态条件查询出订单
    @Override
    public List<MyOrderVo> selectOrder(Long buyerId, Integer state) {

        List<POrder> pOrdersList = new ArrayList<>();
        POrder p = new POrder();
        p.setBuyerId(buyerId);
        if (state!=0){
            //带状态查询订单
            p.setState(state);
            //查询当前用户的所有订单
             pOrdersList = pOrderMapper.select(p);
        } else {
             pOrdersList = pOrderMapper.select(p);
        }
        //创建订单数据容器
        List<MyOrderVo> myOrderVoList =new  ArrayList<MyOrderVo>();
            for (POrder pOrder : pOrdersList) {
                MyOrderVo myOrderVo = new MyOrderVo();
                myOrderVo.setId(pOrder.getId());//获取订单Id
                myOrderVo.setBuyerId(pOrder.getBuyerId());//获取订单购买用户Id
                myOrderVo.setSellerId(pOrder.getSellerId());//获取订单卖方Id
                myOrderVo.setProductId(pOrder.getProductId());//商品Id
                myOrderVo.setPrice(pOrder.getPrice());//获取订单价格
                myOrderVo.setCreatTime(pOrder.getCreatTime());//获取创建时间
                myOrderVo.setUpdateTime(pOrder.getUpdateTime());//获取修改时间
                myOrderVo.setAddress(pOrder.getAddress());//获取地址
                myOrderVo.setState(pOrder.getState());//获取订单状态

                //通过商品Id获取商品信息
                Product product = new Product();
                product.setId(pOrder.getProductId());
                //获取商品信息
                Product myProduct = productMapper.selectOne(product);
                myOrderVo.setPName(myProduct.getName());//获取商品名
                //获取商品图片
                String imgs = myProduct.getImgs();
                String midChar = ";";
                String img = imgs.substring(0,imgs.indexOf(midChar)+ midChar.length()-1);
                myOrderVo.setPImgs(img);
                //将myOrderVo添加到myOrderVoList中
                myOrderVoList.add(myOrderVo);
            }
        return myOrderVoList;
    }
    //查询单个订单
    @Override
    public POrder myOrder(Long buyerId,Long productId){
        POrder p = new POrder();
        p.setBuyerId(buyerId);
        p.setProductId(productId);

        POrder pOrder = pOrderMapper.selectOne(p);
        return pOrder;
    }


    //修改订单状态
    @Override
    public Integer updateOrder(Long buyerId,Long productId, Integer state) {
        POrder p = new POrder();
        p.setId(buyerId);
        p.setState(state);
        int i = pOrderMapper.updateByPrimaryKeySelective(p);
        return i;
    }

}

